package state;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import model.expression.AbstractExpression;
import model.parser.ParserException;

/**
 * The tabs associated with a window.
 * 
 * @author djs22
 *
 */
public class TabState extends Observable{
	
	TabState(int i){
		id = i;
		//IDcount++;
		turtlesMap = new HashMap<Integer, TurtleState>();
		turtlesMap.put(0, new TurtleState(0));
		activeTurtleID = new ArrayList<Integer>();
		activeTurtleID.add(0);
		
		
		variables = new HashMap<String,List<AbstractExpression>>();
	}
	
	/**
	 * the id of this tab
	 */
	public int id;
	
	public static int stId=0;

	/**
	 * the variables that exist in this class
	 */
	public Map<String,List<AbstractExpression>> variables;
	
	/**
	 * the turtles active in this tab
	 */
	public Map<Integer, TurtleState> turtlesMap;
	
	/**
	 * the id of the active turtle
	 */
	private List<Integer> activeTurtleID;
	
	//add turtle if not in the map, add to the active list
	public void addTurtle(int i){
		if (!turtlesMap.containsKey(i)){
			turtlesMap.put(i, new TurtleState(i));
			this.setChanged();
		}
		activeTurtleID.add(i);
	}
	
	public void addTurtles(List<Integer> list){
		//add to the map if not exist
		for(Integer i : list)
			if (!turtlesMap.containsKey(i))
				turtlesMap.put(i, new TurtleState(i));
		
		//set active turtle list
		activeTurtleID = list;
		this.setChanged();
	}
	
	public List<Integer> getActiveTurtle(){
		return activeTurtleID;
	}

	public void clearActiveTurtles() {
		activeTurtleID = new ArrayList<Integer>();
		
	}
	
	//clear screen, all turtles back to 0 0, 
	public void clearScreen(){
		for (Integer i : turtlesMap.keySet()){
			turtlesMap.get(i).setToHome();
		}
		this.setChanged();
	}
	
	public TabState(TurtleState ts) {
		this.id=TabState.stId;
		TabState.stId++;
		this.turtlesMap =new HashMap<Integer,TurtleState>();
		this.turtlesMap.put(ts.getId(),ts);	}
	
	public void addVars(String s,List<AbstractExpression> list){
		variables.put(s, list);
		this.setChanged();
	}
}
